/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jktest;

import javax.swing.UIManager;
import javax.swing.SwingUtilities;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.OceanTheme;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;
import javax.swing.BorderFactory;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.GridLayout;

/**
 *
 * GUI that allows user to choose a theme before starting JKTest (visual aesthetics)
 *
 * @author Royce Feng
 */
public class JKUIChooser implements ActionListener {

    private JKMainGui parent;

    private JDialog frame = new JDialog();
    private JPanel mainPanel = new JPanel();
    private JPanel panel = new JPanel( new GridLayout(7,1) );

    private class KeyStalker implements KeyListener {
        public void keyReleased( KeyEvent e ) {}
        public void keyPressed( KeyEvent e ) {
            switch ( e.getKeyCode() ) {
                case 115: // F4
                case 27: // ESC
                    frame.setVisible(!frame.isVisible());
                    break;
                default:
                    break;
            }
        }
        public void keyTyped( KeyEvent e ) {}
    }

    /**
     *
     * Constructor for JKUIChooser. Creates a frame and a panel for all the different
     * theme choosers. Also creates a button to start the main application.
     *
     */
    public JKUIChooser( JKMainGui parent ) {
        this.parent = parent;
        frame.addKeyListener(new KeyStalker());
        frame.setContentPane(mainPanel);
        frame.setResizable ( false );
        frame.setTitle("Theme Chooser");

        TitledBorder border = BorderFactory.createTitledBorder("Themes");
        panel.setBorder(border);

        mainPanel.add(panel);

        JButton nimbus = new JButton("Nimbus Theme");
        nimbus.addActionListener(this);
        nimbus.addKeyListener(new KeyStalker());
        panel.add(nimbus);

        JButton defaultMetal = new JButton("Default Metal Theme");
        defaultMetal.addActionListener(this);
        defaultMetal.addKeyListener(new KeyStalker());
        panel.add(defaultMetal);

        JButton oceanMetal = new JButton("Ocean Metal Theme");
        oceanMetal.addActionListener(this);
        oceanMetal.addKeyListener(new KeyStalker());
        panel.add(oceanMetal);

        JButton motif = new JButton("Motif Theme");
        motif.addActionListener(this);
        motif.addKeyListener(new KeyStalker());
        panel.add(motif);

        JButton windows = new JButton("Windows Theme");
        windows.addActionListener(this);
        windows.addKeyListener(new KeyStalker());
        panel.add(windows);

        JButton system = new JButton("System Theme");
        system.addActionListener(this);
        system.addKeyListener(new KeyStalker());
        panel.add(system);
        
        JButton crossPlatform = new JButton("Cross Platform Theme");
        crossPlatform.addActionListener(this);
        crossPlatform.addKeyListener(new KeyStalker());
        panel.add(crossPlatform);
        
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        frame.pack();
    }

    /**
     *
     * Sorts by the Action Command and does the seperate tasks. Starts JKTest if Start JKTest
     * is pushed. Otherwise it changes the Look and Feel and updates the ComponentTreeUI for
     * frame and packs the frame in order to make it look nicer.
     * 
     * @param ae The ActionEvent
     */
    public void actionPerformed( ActionEvent ae ) {
        String actionCommand = ae.getActionCommand();
        if ( actionCommand.equals("Nimbus Theme")) {
            try {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            } catch (Exception e) {
            }
        } else if (actionCommand.equals("Default Metal Theme")) {
            try {
                MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (Exception e) {
            }
        } else if (actionCommand.equals("Default Ocean Theme")) {
            try {
                MetalLookAndFeel.setCurrentTheme(new OceanTheme());
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (Exception e) {
            }
        } else if (actionCommand.equals("Motif Theme")) {
            try {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.MotifLookAndFeel");
            } catch (Exception e) {
            }
        } else if (actionCommand.equals("Windows Theme")) {
            try {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            } catch (Exception e) {
            }
        } else if (actionCommand.equals("System Theme")) {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
            }
        } else if (actionCommand.equals("Cross Platform Theme")) {
            try {
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            } catch (Exception e) {
            }
        }
        SwingUtilities.updateComponentTreeUI(frame);
        frame.pack();
        parent.packFrame();
    }

}
